package com.gxa.luckin.admin.controller;

import com.gxa.luckin.admin.service.AdminService;
import com.gxa.luckin.admin.service.impl.AdminServiceImpl;
import com.gxa.luckin.core.exception.BusinessException;
import com.gxa.luckin.core.exception.MessageCode;
import com.gxa.luckin.core.pojo.Admin;
import com.gxa.luckin.core.utils.JsonResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@CrossOrigin
@RequestMapping("/admin")
public class AdminController {
    @Resource
    AdminService adminService;
    @GetMapping("/login")
    public JsonResult login(@RequestParam String username,@RequestParam String password){
        JsonResult jsonResult=new JsonResult();
        Admin login = adminService.login(username, password);
        if(login!=null){
            jsonResult.setCode("1");
            jsonResult.setMsg("登录成功");
            jsonResult.setData(login);
        }else {
            jsonResult.setCode("0");
            jsonResult.setMsg("登录失败");
        }
        return jsonResult;
    }
    @GetMapping("/register")
    public JsonResult register(@RequestParam String username,@RequestParam String password,@RequestParam String repassword) throws BusinessException {
        JsonResult jsonResult=new JsonResult();
        Admin admin=new Admin();
        if(password.equals(repassword)){
            admin.setName(username);
            admin.setPassword(password);
            Admin register = adminService.register(username, password);
            if(register!=null) {
                jsonResult.setCode("1");
                jsonResult.setMsg("创建成功");
                return jsonResult;
            }
            else {
                jsonResult.setCode("0");
                jsonResult.setMsg("创建失败");
                return jsonResult;
            }
        }else {
            throw new BusinessException(MessageCode.ADMIN_ERROR);
        }
    }
}
